package com.zyb.beiojclientsdk.client;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import com.zyb.beiojclientsdk.domain.CodeSandboxRequest;
import com.zyb.beiojclientsdk.domain.CodeSandboxResponse;

import com.zyb.beiojclientsdk.utils.KeyUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 十八岁讨厌编程
 * @date 2023/11/14 15:33
 * @PROJECT_NAME beiapi-interface
 * @description
 */


public class BeiojCodeSandboxClient {

    private String accessKey;

    private String secrectKey;

    public BeiojCodeSandboxClient() {
    }

    public BeiojCodeSandboxClient(String accessKey, String secrectKey) {
        this.accessKey = accessKey;
        this.secrectKey = secrectKey;
    }

    //请求头的map
    public Map<String, String> getHeaderMap(String body) {
        HashMap<String, String> map = new HashMap<>();
        //添加参数
        map.put("accessKey", accessKey);
        map.put("nonce", RandomUtil.randomNumbers(5));
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        map.put("body", body);
        map.put("sign", KeyUtils.getSign(map.toString(), secrectKey));

        return map;

    }

    public CodeSandboxResponse callRemoteCodeSandbox(CodeSandboxRequest request) {
        String jsonStr = JSONUtil.toJsonStr(request);

        Map<String, String> keyMap = getHeaderMap(jsonStr);

        String result = HttpRequest.post("http://192.168.52.129:8080/executeCode")
                .addHeaders(keyMap)
                .body(jsonStr)
                .execute().body();
        return JSONUtil.toBean(result, CodeSandboxResponse.class);
    }

}
